/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TUserDao;
import com.ruipengkj.service.TUserService;
/**
 * 说明：
 * 用户Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TUserServiceImpl implements TUserService{
	@Resource
	private TUserDao TUserDao;//DAO层
	//查询所有
	@Override
	public List<TUser> findService(TUser entity) throws Exception {
		String hql = "select o from TUser o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getCardNum()!=null&&!entity.getCardNum().equals("")){
				whereHQL.append("CardNum").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCardNum()+"%");
			}
			if(entity.getRealName()!=null&&!entity.getRealName().equals("")){
				whereHQL.append("RealName").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getRealName()+"%");
			}
			if(entity.getPwd()!=null&&!entity.getPwd().equals("")){
				whereHQL.append("Pwd").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getPwd()+"%");
			}
			if(entity.getPhone()!=null&&!entity.getPhone().equals("")){
				whereHQL.append("Phone").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getPhone()+"%");
			}
			if(entity.getMobile()!=null&&!entity.getMobile().equals("")){
				whereHQL.append("Mobile").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getMobile()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TUserDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	//根据ID翻页查询
	@Override
	public PageModel<TUser> getScrollService(int firstResult,
			int maxResult, TUser entity) throws Exception {
		String hql = "select o from TUser o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getCardNum()!=null&&!entity.getCardNum().equals("")){
				whereHQL.append("CardNum").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getCardNum()+"%");
			}
			if(entity.getRealName()!=null&&!entity.getRealName().equals("")){
				whereHQL.append("RealName").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getRealName()+"%");
			}
			if(entity.getMobile()!=null&&!entity.getMobile().equals("")){
				whereHQL.append("Mobile").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getMobile()+"%");
			}
			if(null!=entity.getSendCardTime()&&!("").equals(entity.getSendCardTime())){
				whereHQL.append("SendCardTime>=?").append(BaseQuery.AND);
				param.add(entity.getSendCardTime());
			}
			if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
				whereHQL.append("SendCardTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TUserDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//修改
	@Override
	public void modifyService(TUser entity) throws Exception {
		TUserDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TUser entity) throws Exception {
		TUserDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TUserDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TUser getByIdService(String id) throws Exception {
		return TUserDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TUser entity) throws Exception {
		TUserDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TUser entity) throws Exception {
		return TUserDao.saveRetDao(entity);
	}
	//根据会员用户名查询
	@Override
	public TUser getByUserNameService(String UserName) throws Exception {
		if (UserName != null&&!UserName.equals("")) {
			String hql = "select o from TUser o";
			StringBuffer whereHQL = new StringBuffer();
			List<Object> param = new ArrayList<Object>();
				whereHQL.append("o.userName=?");
				param.add(UserName);
			List<TUser> list=TUserDao.findDao(hql, whereHQL.toString(), param, null);
			if(list!=null&&list.size()!=0){
				return list.get(0);
			}
		}
		return null;
	}
	
	//根据会员id查询会员用户名查询
	@Override
	public TUser getUserNameService(String UserID) throws Exception {
			String hql = "select new TUser(o.userID,o.realName)from TUser o";
			StringBuffer whereHQL = new StringBuffer();
			List<Object> param = new ArrayList<Object>();
			if (UserID != null&&!UserID.equals("")) {
				whereHQL.append("o.userID=?");
				param.add(UserID);
			List<TUser> list=TUserDao.findDao(hql, whereHQL.toString(), param, null);
			if(list!=null&&list.size()!=0){
				return list.get(0);
			}
		}
		return null;
	}
	
	//根据会员卡号查询
	@Override
	public TUser getByCardNumService(String CardNum) throws Exception {
		if (CardNum != null&&!CardNum.equals("")) {
			String hql = "select o from TUser o";
			StringBuffer whereHQL = new StringBuffer();
			List<Object> param = new ArrayList<Object>();
			whereHQL.append("o.cardNum=?");
			param.add(CardNum);
			List<TUser> list=TUserDao.findDao(hql, whereHQL.toString(), param, null);
			if(list!=null&&list.size()!=0){
				return list.get(0);
			}
		}
		return null;
	}
	//根据会员手机号查询
	@Override
	public TUser getByMobileService(String mobile) throws Exception {
		if (mobile != null&&!mobile.equals("")) {
			String hql = "select o from TUser o";
			StringBuffer whereHQL = new StringBuffer();
			List<Object> param = new ArrayList<Object>();
			whereHQL.append("o.mobile=?");
			param.add(mobile);
			List<TUser> list=TUserDao.findDao(hql, whereHQL.toString(), param, null);
			if(list!=null&&list.size()!=0){
				return list.get(0);
			}
		}
		return null;
	}
	@Override
	public boolean modifyRechargeService(TUser TUser) throws Exception {
		if(TUser!=null){
			String hql = "select o from TUser o";
			StringBuffer whereHQL = new StringBuffer();
			List<Object> param = new ArrayList<Object>();
			if(TUser.getPhone()!=null&&!TUser.getPhone().equals("")){
				//根据手机号码充值
				whereHQL.append("o.phone=?");
				param.add(TUser.getPhone());
				List<TUser> list=TUserDao.findDao(hql, whereHQL.toString(), param, null);
				if(list!=null&&list.size()!=0){
					TUser user=list.get(0);
					user.setMoney(user.getMoney()+TUser.getMoney());
					TUserDao.modifyDao(user);
					return true;
				}
			}else if(TUser.getCardNum()!=null&&!TUser.getCardNum().equals("")){
				//根据会员卡充值
				whereHQL.append("o.cardNum=?");
				param.add(TUser.getCardNum());
				List<TUser> list=TUserDao.findDao(hql, whereHQL.toString(), param, null);
				if(list!=null&&list.size()!=0){
					TUser user=list.get(0);
					user.setMoney(user.getMoney()+TUser.getMoney());
					TUserDao.modifyDao(user);
					return true;
				}
			}
		}
		return false;
	}
	public TUserDao getTUserDao() {
		return TUserDao;
	}

	public void setTUserDao(TUserDao TUserDao) {
		this.TUserDao = TUserDao;
	}
}